<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div style="margin-top: 20px;">
</div>
<form class="layui-form" id="admin">

    <div class="layui-form-item">
        <label class="layui-form-label">匿名</label>
        <div class="layui-input-inline">
            <input lay-filter="anonymous" name="anonymous" class="layui-input" type="radio" value="1" title="是" checked>
            <input lay-filter="anonymous" name="anonymous" class="layui-input" type="radio" value="0" title="否">
        </div>
    </div>
    <div class="layui-form-item userinfo" style="display:none">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-inline">
            <input name="member_nickname" placeholder="请输入" autocomplete="off" class="layui-input" type="text">
        </div>
    </div>

    <div class="layui-upload userinfo" id="upload-thumb" style="display:none">
        <label class="layui-form-label">头像</label>
        <button type="button" class="layui-btn" id="thumb">上传头像</button>
        <div class="layui-upload-list">
            <label class="layui-form-label"></label>
            <img class="layui-upload-img" id="demo1" width="150" height="150">
            <p id="demoText"></p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">POI选择</label>
        <div class="layui-input-inline">
            <!--select 选择poi-->
            <!--<select name="city" class="layui-input-inline" lay-filter="city">-->
                <!--<option value="">请选择城市</option>-->
                <!--{volist name='city' id='value'}-->
                <!--<option value="{$value.id}">{$value.name}</option>-->
                <!--{/volist}-->
            <!--</select>-->
            <!--<select name="poi" class="layui-input-inline" lay-filter="poi">-->
                <!--<option value="">请选择poi</option>-->
            <!--</select>-->
            <!--input 输入poi-->
            <input placeholder="请输入poi" lay-verify="required" class="layui-input" name="poi" type="number">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">评分</label>
        <div class="layui-input-inline" style="width:350px;">
            <input placeholder="请输入评分" lay-verify="required" class="layui-input" name="score" type="number" min="0" max="5" step="1">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">评论内容</label>
        <div class="layui-input-inline" style="width:350px;">
            <textarea placeholder="请输入内容" lay-verify="required" class="layui-textarea" name="content"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">评论图片</label>
        <div class="layui-input-inline">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="upload1">上传评论图片</button>
                <blockquote class="layui-elem-quote layui-quote-nm" style="width:315px;margin-top: 10px;">
                    预览图：
                    <div class="layui-upload-list" id="seeupload1">
                        <span class="uploadimgbox"></span>
                    </div>
                </blockquote>
            </div>
        </div>
    </div>
    <input id="upload-thumb1" type="hidden" name="other_images">


    <div class="layui-form-item">
        <label class="layui-form-label">评论时间</label>
        <div class="layui-input-inline">
            <input name="comment_time" id="comment_time" placeholder="请输入" autocomplete="off" class="layui-input"  type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>
<script>
    layui.use('upload', function(){
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#thumb' //绑定元素
            ,url: "{:url('common/upload')}" //上传接口
            ,done: function(res){
                //上传完毕回调
                layer.closeAll('loading');
                if(res.code == 2) {
                    $('#demo1').attr('src',res.src);
                    $('#upload-thumb').append('<input type="hidden" name="thumb" value="'+ res.id +'">');
                } else {
                    layer.msg(res.msg);
                }
            }
            ,error: function(){
                //请求异常回调
                //演示失败状态，并实现重传
                layer.closeAll('loading');
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            ,before:function () {
                layer.load(2);
            }
        });
    });

    //文件上传
    layui.use('upload', function(){
        var upload = layui.upload;
        //多图片上传
        upload.render({
            elem: '#upload1'
            ,url: "{:url('common/upload')}"
            ,multiple: true
            ,accept:"images"
            ,size:"50000"
            ,done: function(res){
                console.log(res);
                //上传完毕回调
                layer.closeAll('loading');
                if(res.code == 2) {
                    $('#seeupload1').append('<span class="uploadimgbox"><img class="layui-upload-img" src="'+res.src+'" style="width:100px;"><i class="layui-icon">&#x1006;</i></span>');
                    $('#upload-thumb1').val($('#upload-thumb1').val()+","+res.src);
                } else {
                    layer.msg(res.msg);
                }
            }
            ,error: function(){
                //请求异常回调
                //演示失败状态，并实现重传
                layer.closeAll('loading');
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            ,before:function () {
                layer.load(2);
            }
        });
    });

    $("#seeupload1").on("click",".layui-icon",function(){
        $(this).parent().remove();
        thumb("seeupload1",1);
    });
    function thumb(obj,n){
        var thumbs="";
        $("#"+obj).find("img").each(function(){
            var url=$(this).attr("src");
            thumbs+=","+url;
        });
        $("#upload-thumb"+n).val(thumbs);
    }

    layui.use(['form','laydate'], function(){
        var  form = layui.form ;
        $ = layui.jquery ;
        laydate = layui.laydate;
        laydate.render({     //创建时间选择框
            elem: '#comment_time' //指定元素
            ,format: 'yyyy-MM-dd HH:mm'
            ,type: 'datetime'
        });
    });

</script>
<script>
    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $(window).on('load', function() {
            form.on('submit(admin)', function(data) {
                var is_anonymous = $("input[name=anonymous]:checked").val();
                if(is_anonymous==0){
                    var nickname = $("input[name=member_nickname]").val();
                    if(nickname=='' || nickname==undefined){
                        layer.msg("请填写昵称");
                        return false;
                    }
                }
                $.ajax({
                    url:"{:url('admin/poi/virtualcomment')}",
                    data:$('#admin').serialize(),
                    type:'post',
                    async: false,
                    success:function(res) {
                        layer.msg(res.msg);
                        if(res.code == 200) {
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },1500)
                        }
                    }
                })
                return false;
            });
            form.on('radio(anonymous)',function(data){
                id = $(this).val();
                if(id=='1'){
                    $(".userinfo").hide()
                }else{
                    $(".userinfo").show()

                }
            })


            form.on('select(city)',function(data){
                var city = data.value;
                $.post('/admin/poi/getPoiByCity',{id:city},function(data){
                    console.log(data)
                    if(data.code==200){
                        $("select[name=poi]").children().remove();
                        var str = "<option value=''>请选择poi</option>";
                        $.each(data.data,function(i,v){
                            str += '<option value="'+v.id+'">'+v.name+'</option>';
                        })

                        $("select[name=poi]").append(str);
                        form.render();
                    }
                },'json')
            })
        });
    });
</script>
</body>
</html>
